/**
 * 
 */

package uranium.db.tabelle;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import uranium.db.ConnectionManager;
import uranium.db.DBManager;
import uranium.db.DbBrand;
import uranium.db.tabelle.eccezioni.IDClienteNonImpostato;

/**
 * @author hunterbit
 */
public class Cliente
{
	private String		nome;
	private String		cognome;
	//private String		n_tel;
	//private String		via;
	//private String		email;
	private int			id;
	private DBManager	dbm;

	public Cliente(String nome, String cognome, DBManager dbm)
	{
		this.nome = nome;
		this.cognome = cognome;
		this.id = -1;
		this.dbm = dbm;
	}

	/**
	 * @param dbm2
	 */
	public Cliente(DBManager dbm)
	{
		this.dbm = dbm;
	}

	public String getCognome()
	{
		return cognome;
	}

	public void setCognome(String cognome)
	{
		this.cognome = cognome;
	}

	/*public String getEmail()
	{
		return email;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}

	public String getTelefono()
	{
		return n_tel;
	}

	private int getNewID()
	{
		ResultSet rs = null;
		int newID = 0;
		String query = "";
		if (DBManager.getDbBrand() == DbBrand.HSQLDB)
		{
			query = "SELECT MAX(ID) FROM clienti";
		}
		try
		{
			rs = dbm.executeQuery(query);
			rs.next();
			newID = (rs.getInt(1)) + 1;
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		return newID;
	}

	public void setTelefono(String n_tel)
	{
		this.n_tel = n_tel;
	}

	public String getNome()
	{
		return nome;
	}

	public void setNome(String nome)
	{
		this.nome = nome;
	}

	public String getVia()
	{
		return via;
	}

	public void setVia(String via)
	{
		this.via = via;
	}

	public int insertCliente()
	{
		String insert = "";
			insert = "INSERT INTO clienti (id,nome,cognome,n_tel,via,email) VALUES(" + getNewID() + ",'" + nome + "','"
						+ cognome + "','" + n_tel + "','" + via + "','" + email + "')";
		

		return dbm.executeUpdate(insert);
		
	}

	public int updateCliente() throws IDClienteNonImpostato
	{
		if (id == -1) throw new IDClienteNonImpostato();
		Statement st = null;
		int aggiornati = 0;
		String update = "";

		update = "UPDATE clienti SET nome='" + nome + "', cognome='" + cognome + "', n_tel='" + n_tel + "', via='"
					+ via + "', email='" + email + "' WHERE id=" + id;

		aggiornati = dbm.executeUpdate(update);
		return aggiornati;
	}

	public int deleteCliente() throws IDClienteNonImpostato
	{
		String delete = "";
		Statement st = null;
		int cancellati = 0;
		if (id == -1) throw new IDClienteNonImpostato();

		if (DbBrand.POSTGRESQL == DBManager.getDbBrand() || DbBrand.HSQLDB == DBManager.getDbBrand())
		{
			delete = "DELETE FROM clienti WHERE id=" + this.id;
		}
		cancellati = dbm.executeUpdate(delete);

		return cancellati;
	}

	/**
	 * @return
	 */
	/*public ResultSet getAllClienti()
	{
		String query = "SELECT * FROM clienti ORDER BY nome ASC";
		ResultSet rs = null;
		rs = dbm.executeQuery(query);
		return rs;
	}

	/**
	 * @param idCliente
	 */
	/*public void setID(int idCliente)
	{
		this.id = idCliente;

	}

	/**
	 * 
	 */
	/*public void loadDati() throws IDClienteNonImpostato
	{
		String query = "SELECT * FROM clienti WHERE ID=" + this.id;
		ResultSet rs = null;
		rs = dbm.executeQuery(query);
		try
		{
			rs.next();
			this.cognome = rs.getString("cognome");
			this.email = rs.getString("email");
			this.n_tel = rs.getString("tel");
			this.nome = rs.getString("nome");
			this.via = rs.getString("indirizzo");
		} catch (SQLException e)
		{
			e.printStackTrace();
		} finally
		{
			if (rs != null)
			{
				try
				{
					rs.close();
				} catch (SQLException e)
				{
					e.printStackTrace();
				}
			}
		}

	}

	/**
	 * @param idCliente
	 * @return
	 */
	public static Cliente getCliente(int idCliente)
	{
		
		return null;
	}

}
